﻿#region

using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Vin.Extension.JwtBearer.Constants;
using Vin.Tool.Core.ConvertCategory;
using Vin.Tool.Domain.ExceptionModel;

#endregion

namespace Vin.Extension.JwtBearer.Extension;

public static class HttpContextExtension
{
    public static string? GetUserId(this HttpContext httpContext)
    {
        return httpContext.User.Claims.FirstOrDefault(c => c.Type == VinClaimTypesConstant.UserId)?.Value;
    }
    
    public static string? GetPrefix(this HttpContext httpContext)
    {
        return httpContext.User.Claims.FirstOrDefault(c => c.Type == VinClaimTypesConstant.Prefix)?.Value;
    }

    #region RealAccountId

    public static string? GetRealAccountId(this HttpContext httpContext)
    {
        return httpContext.User.Claims.FirstOrDefault(c => c.Type == VinClaimTypesConstant.RealAccountId)?.Value;
    }

    public static T GetRealAccountId<T>(this HttpContext httpContext)
    {
        var value = GetRealAccountId(httpContext) ?? throw new UnauthorizedException();
        return value.To<T>();
    }

    #endregion

    #region RealUserId

    public static string? GetRealUserId(this HttpContext httpContext)
    {
        return httpContext.User.Claims.FirstOrDefault(c => c.Type == VinClaimTypesConstant.RealUserId)?.Value;
    }

    public static T GetRealUserId<T>(this HttpContext httpContext)
    {
        var value = GetRealUserId(httpContext) ?? throw new UnauthorizedException();
        return value.To<T>();
    }

    #endregion

    public static string? GetTerminalType(this HttpContext httpContext)
    {
        return httpContext.User.Claims.FirstOrDefault(c => c.Type == VinClaimTypesConstant.TerminalType)?.Value;
    }

    public static string? GetUserName(this HttpContext httpContext)
    {
        return httpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
    }
    
    public static string? GetAccountName(this HttpContext httpContext)
    {
        return httpContext.User.Claims.FirstOrDefault(c => c.Type == VinClaimTypesConstant.AccountName)?.Value;
    }

    public static string? GetPlatform(this HttpContext httpContext)
    {
        return httpContext.User.Claims.FirstOrDefault(c => c.Type == VinClaimTypesConstant.Platform)?.Value;
    }
    
    public static string? GetPhone(this HttpContext httpContext)
    {
        return httpContext.User.Claims.FirstOrDefault(c => c.Type == VinClaimTypesConstant.Phone)?.Value;
    }
}